//package com.geeklan.smart.ui.main.adapter;
//
//import android.app.Activity;
//import android.content.Context;
//import android.graphics.Color;
//import android.graphics.drawable.ColorDrawable;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.FrameLayout;
//import android.widget.Toast;
//
//import androidx.annotation.NonNull;
//import androidx.appcompat.content.res.AppCompatResources;
//import androidx.recyclerview.widget.RecyclerView;
//
//import com.bumptech.glide.Glide;
//import com.geeklan.base.utils.MLog;
//import com.geeklan.smart.R;
//import com.geeklan.smart.data.model.Device;
//import com.geeklan.smart.databinding.HomeDeviceItemBinding;
//import com.geeklan.smart.permission.PermissionManager;
//import com.geeklan.smart.ui.device.DeviceInfoActivity;
//import com.geeklan.smart.ui.device.VideoCallActivity;
//import com.geeklan.smart.utils.GlideUtils;
//import com.geeklan.smart.widget.RoundedImageView;
//
//import java.util.List;
//
//public class HomeDeviceListAdapter extends RecyclerView.Adapter<HomeDeviceListAdapter.ViewHolder> {
//    private Activity mContext;
//    private List<Device> mData;
//
//    private String playDeviceId = "";
//
//    public void setPlayDeviceId(String playDeviceId) {
//        this.playDeviceId = playDeviceId;
//    }
//
//    private boolean isSideline = false;
//
//    public HomeDeviceListAdapter(Activity context, List<Device> data) {
//        this.mData = data;
//        this.mContext = context;
//    }
//
//    @NonNull
//    @Override
//    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        HomeDeviceItemBinding homeDeviceItemBinding = HomeDeviceItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
//        return new ViewHolder(homeDeviceItemBinding);
//    }
//
//    @Override
//    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        Device device = mData.get(position);
//        String deviceId = device.getDeviceId();
//        String deviceId_4;
//        try {
//            deviceId_4 = deviceId.substring(deviceId.length() - 4);
//        } catch (Exception e) {
//            deviceId_4 = "";
//        }
//        holder.mBinding.tvPetName.setText(device.getDeviceName() + "(" + deviceId_4 + ")");
//
//        holder.mBinding.ivPhoto.setVisibility(playDeviceId.equals(deviceId) ? View.INVISIBLE : View.VISIBLE);
//        holder.mBinding.llLookTa.setVisibility(playDeviceId.equals(deviceId) ? View.INVISIBLE : View.VISIBLE);
//        holder.mBinding.remoteVideoViewContainer.setVisibility(playDeviceId.equals(deviceId) ? View.VISIBLE : View.INVISIBLE);
//        holder.mBinding.ivCloseVideo.setVisibility(playDeviceId.equals(deviceId) ? View.VISIBLE : View.INVISIBLE);
//        holder.mBinding.ivVideoFullScreen.setVisibility(playDeviceId.equals(deviceId) ? View.VISIBLE : View.INVISIBLE);
//        holder.mBinding.tvAuto.setText(device.getAutoFeedState() == 0 ? mContext.getString(R.string.not_open) : mContext.getString(R.string.open));
//        holder.mBinding.tvSolidFoodState.setText(device.getSolidFoodState() == 1 ? mContext.getString(R.string.sufficient) : mContext.getString(R.string.impatient));
//        holder.mBinding.tvCatStripsState.setText(device.getCatStripsState() == 1 ? mContext.getString(R.string.sufficient) : mContext.getString(R.string.impatient));
//        if (device.getSolidFoodState() != 1) {
//            holder.mBinding.tvSolidFoodState.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_home_device_food_urgent, 0);
//        } else {
//            holder.mBinding.tvSolidFoodState.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
//        }
//
//        if (device.getCatStripsState() != 1) {
//            holder.mBinding.tvCatStripsState.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_home_device_food_urgent, 0);
//        } else {
//            holder.mBinding.tvCatStripsState.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
//        }
//        holder.mBinding.ivCloseVideo.setOnClickListener(v -> {
//            playDeviceId = "";
//            notifyItemChanged(position);
//            holder.mBinding.ivPhoto.setVisibility(View.VISIBLE);
//            holder.mBinding.ivPhoto1.setVisibility(View.VISIBLE);
//            holder.mBinding.remoteVideoViewContainer.setVisibility(View.GONE);
//            holder.mBinding.remoteVideoViewContainer1.setVisibility(View.GONE);
//            initRtc.closeVideo();
//            notifyItemChanged(position);
//        });
//        holder.mBinding.llLookTa.setOnClickListener(v -> {
//            holder.mBinding.remoteVideoViewContainer.removeAllViews();
//            holder.mBinding.remoteVideoViewContainer1.removeAllViews();
//            if (!PermissionManager.checkStoragePermissions(mContext)) {
//                mContext.requestPermissions(PermissionManager.getStoragePermissions(), 1);
//            }else {
//                if (device.getOnlineStatus() == 1) {
//                    playDeviceId = deviceId;
//                    notifyItemChanged(position);
//                    holder.mBinding.remoteVideoViewContainer.setVisibility(View.VISIBLE);
//                    holder.mBinding.remoteVideoViewContainer1.setVisibility(View.VISIBLE);
//                    initRtc.init(holder.mBinding.remoteVideoViewContainer, holder.mBinding.remoteVideoViewContainer1, holder.mBinding.ivPhoto, holder.mBinding.ivPhoto1, deviceId);
//                } else {
//                    Toast.makeText(mContext, "设备不在线", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
//
//        holder.mBinding.remoteVideoViewContainer1.setOnClickListener(v -> {
//            if(initRtc.switchLines(isSideline)){
//                playDeviceId = deviceId;
//                notifyItemChanged(position);
//                isSideline = !isSideline;
//            }
//        });
//
//        holder.mBinding.ivVideoFullScreen.setOnClickListener(v -> {
//            initRtc.closeVideo();
//            notifyItemChanged(position);
//            VideoCallActivity.startActivity(mContext, deviceId);
//        });
//
//        holder.mBinding.clDeviceInfo.setOnClickListener(v -> {
//            DeviceInfoActivity.startActivity(mContext, device.getDeviceId(), device.getDeviceName(), device.getVideoPreviewImage(), device.getVideoPreviewImage1());
//        });
//
//        if (device.getOnlineStatus() != 1) {
//            MLog.i("设备不在线");
//            holder.mBinding.onlineView.setBackground(AppCompatResources.getDrawable(mContext, R.drawable.layout_red_radius));
//            holder.mBinding.tvDeviceOnline.setText(mContext.getString(R.string.device_offline));
//        } else {
//            MLog.i("设备在线");
//            holder.mBinding.onlineView.setBackground(AppCompatResources.getDrawable(mContext, R.drawable.layout_green_spot));
//            holder.mBinding.tvDeviceOnline.setText(mContext.getString(R.string.device_online));
//        }
//
//        if (device.getVideoPreviewImage() != null) {
//            GlideUtils.loadRoundedImage(mContext, device.getVideoPreviewImage(), holder.mBinding.ivPhoto);
//        }else {
//            holder.mBinding.ivPhoto.setImageDrawable(new ColorDrawable(Color.BLACK));
//        }
//
//        if (device.getVideoPreviewImage1() != null) {
//            GlideUtils.loadRoundedImage(mContext, device.getVideoPreviewImage1(), holder.mBinding.ivPhoto1);
//        }else {
//            holder.mBinding.ivPhoto1.setImageDrawable(new ColorDrawable(Color.BLACK));
//        }
//    }
//
//    @Override
//    public int getItemCount() {
//        return mData.size();
//    }
//    @Override
//    public int getItemViewType(int position) {
//        return position; // 每个 item 拥有唯一类型，等于关闭复用
//    }
//
//    // 自定义 ViewHolder
//    static class ViewHolder extends RecyclerView.ViewHolder {
//        HomeDeviceItemBinding mBinding;
//
//        public ViewHolder(@NonNull HomeDeviceItemBinding mBinding) {
//            super(mBinding.getRoot());
//            this.mBinding = mBinding;
//        }
//    }
//
//    private InitRtc initRtc;
//
//    public void setInitRtc(InitRtc initRtc) {
//        this.initRtc = initRtc;
//    }
//
//    public interface InitRtc {
//        void init(FrameLayout frameLayout1, FrameLayout frameLayout2, RoundedImageView ivPhoto, RoundedImageView ivPhoto1, String deviceId);
//
//        void closeVideo();
//
//        void isPlay();
//
//        boolean switchLines(boolean isSideline);
//    }
//}
